import axios from "axios";
import { useUserStore } from "../pinia/user";
const baseURL = "http://121.89.205.189:3000/admin/";

const request = axios.create({
  baseURL,
  timeout: 5000,
});

// #请求拦截器
request.interceptors.request.use(
  (config) => {
    const userStore = useUserStore();
    config.headers.token = userStore.token;
    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

// #响应拦截器
request.interceptors.response.use(
  (res) => {
    console.log(res, "拦截器");
    ElMessage({
      message: res.data.message,
      type: res.data.code == "200" ? "success" : "error",
    });
    return res;
  },
  (err) => {
    return Promise.reject(err);
  }
);

export function get<T>(url: string, data?: T) {
  return request({
    url,
    method: "get",
    params: data,
  });
}
export function post<T>(url: string, data?: T) {
  return request({
    url,
    method: "post",
    data,
  });
}
